﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BtLabelTool;
using BtLabelTool.UhfHelper;

namespace BtLabelBLL
{
   
    public class RfidBLL
    {
        private readonly object RfidLock = new object();
        private BtLabelTool.UhfHelper.UHFAPIOfUsb uhfHelper = new BtLabelTool.UhfHelper.UHFAPIOfUsb();
        /// <summary>
        /// 打开读卡器
        /// </summary>
        /// <returns></returns>
        public bool OpenRfid()
        {
           return  uhfHelper.Open();
        }
        /// <summary>
        /// 关闭读卡器
        /// </summary>
        public bool CloseRfid()
        {
            return uhfHelper.Close();
        }
        /// <summary>
        /// 读取标签
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<string> GetLabels(double delay)
        {
            try
            {
                lock (RfidLock)
                {
                    string filerData = "";
                    string accessPwd = "00000000";
                    //过滤----------------------------------
                    int filerBank = 0;
                    byte[] filerBuff = DataConvert.HexStringToByteArray(filerData);
                    int filerPtr = 32;
                    int filerLen = filerBuff.Length * 8;
                    //----------
                    byte[] pwd = DataConvert.HexStringToByteArray(accessPwd);
                    //0 Reserved; 1 EPC ; 2 TID 
                    int bank = 1;
                    int Ptr = 2;
                    int leng = 3;
                    List<string> labels = new List<string>();
                    DateTime time1 = DateTime.Now.AddMilliseconds(delay);
                    while (time1 > DateTime.Now && labels.Count < 1)
                    {
                        string result = uhfHelper.ReadData(pwd, (byte)filerBank, filerPtr, filerLen, filerBuff, (byte)bank, Ptr, leng);
                        if (result != "" && labels.Contains(result.Trim()) == false)
                        {
                            labels.Add(result.Trim());
                        }
                        CommonHelper.Delay(100);
                    }
                    return labels;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 写入标签
        /// </summary>
        /// <param name="mac"></param>
        public bool WriteRfid(string mac)
        {
            try
            {
                lock (RfidLock)
                {
                    string filerData = "";
                    string accessPwd = "00000000";
                    //过滤----------------------------------
                    int filerBank = 1;
                    byte[] filerBuff = DataConvert.HexStringToByteArray(filerData);
                    int filerPtr = 32;
                    int filerLen = filerBuff.Length * 8;
                    //----------
                    byte[] pwd = DataConvert.HexStringToByteArray(accessPwd);
                    //0 Reserved; 1 EPC ; 2 TID 
                    int bank = 1;
                    int Ptr = 2;
                    int leng = 3;
                    byte[] uDatabuf = DataConvert.HexStringToByteArray(mac.Replace(" ", ""));
                    bool result = uhfHelper.WriteData(pwd, (byte)filerBank, filerPtr, filerLen, filerBuff, (byte)bank, Ptr, (byte)leng, uDatabuf);
                    if (result == false)
                    {
                        return false;
                    }
                    List<string> labels = GetLabels(1);
                    if (labels.Contains(mac.Trim()) == false)
                    {
                        return false;
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 点亮标签
        /// </summary>
        /// <param name="addr"></param>
        /// <returns></returns>
        public void LighteRfid()
        {
            try
            {
                lock (RfidLock)
                {
                    string filerData = "";
                    string accessPwd = "00000000";
                    //过滤----------------------------------
                    int filerBank = 1;
                    byte[] filerBuff = DataConvert.HexStringToByteArray(filerData);
                    int filerPtr = 32;
                    int filerLen = filerBuff.Length * 8;
                    //-----------------------------------------
                    byte[] pwd = DataConvert.HexStringToByteArray(accessPwd);
                    //0 Reserved; 1 EPC
                    int bank = 0;
                    int Ptr = 4;
                    int leng = 1;
                    string result = uhfHelper.ReadData(pwd, (byte)filerBank, filerPtr, filerLen, filerBuff, (byte)bank, Ptr, leng);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
